package com.bawei.monthdemo2207a.model.http

import com.blankj.utilcode.util.SPUtils
import com.tencent.mmkv.MMKV
import com.zyj.retrofit.adapter.FlowCallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.UUID
import java.util.concurrent.TimeUnit

/**
 * 作者：赵亮
 * 时间：2024/12/26 11:17
 * 功能：
 */
object RetrofitManager {

    // OkHttp
    private val client = OkHttpClient.Builder()
        .connectTimeout(5, TimeUnit.MINUTES)
        .readTimeout(5, TimeUnit.MINUTES)
        .writeTimeout(5, TimeUnit.MINUTES)
        .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
        .addInterceptor { chain ->
            chain.proceed(
                chain.request().newBuilder()
                    .header("token", SPUtils.getInstance().getString("token"))
                    .header("atoken", MMKV.defaultMMKV().decodeString("token").toString())
                    .header("sign", UUID.randomUUID().toString())
                    .build()
            )
        }
        .build()

    // Retrofit
    private val retrofit = Retrofit.Builder()
        .baseUrl("http://0/")
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(FlowCallAdapterFactory.create())
        .build()

    // apiService
    val apiService = retrofit.create(ApiService::class.java)
}